package com.team.teamproject.ui.homepage;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.team.teamproject.R;
import com.team.teamproject.adapter.MyHomePageBottomAdapter;
import com.team.teamproject.adapter.MyHomePageModAdapter;
import com.team.teamproject.bean.Banners;
import com.team.teamproject.bean.Data_HomePageBottom;
import com.team.teamproject.bean.Mod_Homepage_Data;
import com.team.teamproject.utils.HttpUtils;
import com.team.teamproject.utils.ListHeightUtils;
import com.team.teamproject.utils.ParseJson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/11/7.
 */
public class PagerFragment_JingXuan extends Fragment {
    //精选界面---------
    //@InjectView(R.id.jingxuan_vp)
    //ViewPager jingxuanVp;
   @InjectView(R.id.gv)
   GridView gv;
    @InjectView(R.id.lv)
    ListView lv;
    List<Data_HomePageBottom.ItemsBean> list;
    private Handler handler = new Handler();
    private MyHomePageBottomAdapter bottomAdapter;
    private MyHomePageModAdapter modAdapter;
    //刷新数据
    PullToRefreshScrollView mPullToRefreshScrollView;
    ScrollView scrollView;
    //刷新
    //精选
    //顶部URL
    String topUrl="http://api.liwushuo.com/v2/banners";
    //中间URL
    String modUrl = "http://api.liwushuo.com/v2/secondary_banners?gender=1&generation=1";
    // 底部Url

    int id;
    int limit = 20;
    String bottomUrl;
    Banner banner;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pager_fragment_jingxuan, null);
        banner= (Banner) view.findViewById(R.id.banner);
        ButterKnife.inject(this, view);
        mPullToRefreshScrollView= (PullToRefreshScrollView) view.findViewById(R.id.PullToRefreshScrollView);
        mPullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        scrollView=mPullToRefreshScrollView.getRefreshableView();
        //获取传来的Id
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = bundle.getInt("id");
            Log.i("aaa", "精选底部Id" + id);
        }
        bottomUrl = "http://api.liwushuo.com/v2/channels/" + id + "/items_v2?limit=" + limit + "&ad=2&gender=1&offset=0&generation=1";
        Log.i("aaa", "精选底部Url--->" + bottomUrl);
        //数据源
        list=new ArrayList<>();
        //获取数据
        getData();
        //数据源
        bottomAdapter = new MyHomePageBottomAdapter(getActivity(), list);
        //刷新数据
        refreshData();
        return view;
    }

    private void refreshData() {
        mPullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getData();
                bottomAdapter.notifyDataSetChanged();
                mPullToRefreshScrollView.onRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                limit+=10;
                if (limit>50){
                    Toast.makeText(getActivity(), "已到达底部", Toast.LENGTH_SHORT).show();
                    mPullToRefreshScrollView.onRefreshComplete();
                    return;
                }
                  Log.i("aaa","上拉加载前id--->"+id);
                Log.i("aaa","上拉加载前limit--->"+limit);
                bottomUrl="http://api.liwushuo.com/v2/channels/"+id+"/items_v2?limit="+limit+"&ad=2&gender=1&offset=0&generation=1";
                //上拉刷新后
                Log.i("aaa","上拉后Url---->"+bottomUrl);
                getData();
                bottomAdapter.notifyDataSetChanged();
                mPullToRefreshScrollView.onRefreshComplete();
            }
        });
    }


    private void getData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                //顶部
                String jsonTop = HttpUtils.getHttpUtils().getJson(topUrl);
                //中间
                String jsonMod = HttpUtils.getHttpUtils().getJson(modUrl);
                //底部
                String json = HttpUtils.getHttpUtils().getJson(bottomUrl);
                if (json != null) {
                    //顶部
                    List<Banners> list = ParseJson.fromToHomeTop(jsonTop);
                    final List<Banners.BannersBean> banners = list.get(0).getBanners();
                    // 中间
                     List<Mod_Homepage_Data> modList = ParseJson.fromToHomeMod(jsonMod);
                    final List<Mod_Homepage_Data.SecondaryBannersBean> beanList = modList.get(0).getSecondary_banners();
                    //底部
                     List<Data_HomePageBottom> datas = ParseJson.fromToHomeBottom(json);
                    final List<Data_HomePageBottom.ItemsBean> items = datas.get(0).getItems();
                    if (!(datas.size() == 0)) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //顶部
                                List<String> images = new ArrayList<String>();

                                for (Banners.BannersBean s: banners){
                                    String image_url = s.getImage_url();
                                    Log.i("aaa","轮播图--->"+image_url);
                                    images.add(image_url);

                                }
                                //设置轮播图
                                banner.setImages(images).setImageLoader(new GlideImageLoader()).start();
                               banner.setOnBannerClickListener(new OnBannerClickListener() {
                                   @Override
                                   public void OnBannerClick(int position) {
                                       //点击轮播图
                                       Intent intent = new Intent();
                                       intent.setClass(getActivity(),HomePage_XiangQingActivity.class);
                                       int id = banners.get(position).getTarget_id();
                                       intent.putExtra("id",id+"");
                                       startActivity(intent);
                                   }
                               });
                                //中间
                                modAdapter = new MyHomePageModAdapter(getActivity(), beanList);
                                gv.setAdapter(modAdapter);
                               gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                   @Override
                                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                       //中间点击事件
                                       int id1 = beanList.get(position).getId();
                                       Intent intent = new Intent();
                                       intent.putExtra("id", id1 + "");
                                       Log.i("aaa","中间id--->"+id1);
                                       intent.setClass(getActivity(), HomePage_XiangQingActivity.class);
                                       startActivity(intent);
                                   }
                               });
                                //底部
                                PagerFragment_JingXuan.this.list.addAll(items);
                                ListHeightUtils.setListViewHeightBasedOnChildren(lv);
                                lv.setAdapter(bottomAdapter);
                                lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                    @Override
                                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                        Data_HomePageBottom.ItemsBean itemsBean = items.get(position);
                                        int idItems = itemsBean.getId();
                                        Intent intent = new Intent();
                                        intent.putExtra("id", idItems + "");
                                        intent.setClass(getActivity(), HomePage_XiangQingActivity.class);
                                        startActivity(intent);
                                    }
                                });
                            }
                        });
                    } else {
                        Toast.makeText(getActivity(), "数据获取失败。。。", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(getActivity(), "请检查网络。。。", Toast.LENGTH_SHORT).show();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
